package com.baichen;

import java.lang.reflect.Field;

/**
 * Around通知处理器
 * 完全控制目标方法的执行，包括前置、后置和异常处理
 */
public class AroundHandler implements MyHandler {
    
    private MyInterface target;
    private String beforeLogic;
    private String afterLogic;
    private String exceptionLogic;
    private boolean shouldExecuteTarget;
    
    /**
     * 构造函数
     * @param target 目标对象
     * @param beforeLogic 前置逻辑
     * @param afterLogic 后置逻辑
     * @param exceptionLogic 异常处理逻辑
     * @param shouldExecuteTarget 是否执行目标方法
     */
    public AroundHandler(MyInterface target, String beforeLogic, String afterLogic, 
                        String exceptionLogic, boolean shouldExecuteTarget) {
        this.target = target;
        this.beforeLogic = beforeLogic;
        this.afterLogic = afterLogic;
        this.exceptionLogic = exceptionLogic;
        this.shouldExecuteTarget = shouldExecuteTarget;
    }
    
    /**
     * 构造函数 - 使用默认的around逻辑
     * @param target 目标对象
     */
    public AroundHandler(MyInterface target) {
        this(target, 
             "System.out.println(\"Around advice: 方法执行前\");",
             "System.out.println(\"Around advice: 方法执行后\");",
             "System.out.println(\"Around advice: 方法执行异常 - \" + e.getMessage());",
             true);
    }
    
    /**
     * 构造函数 - 自定义是否执行目标方法
     * @param target 目标对象
     * @param shouldExecuteTarget 是否执行目标方法
     */
    public AroundHandler(MyInterface target, boolean shouldExecuteTarget) {
        this(target, 
             "System.out.println(\"Around advice: 方法执行前\");",
             "System.out.println(\"Around advice: 方法执行后\");",
             "System.out.println(\"Around advice: 方法执行异常 - \" + e.getMessage());",
             shouldExecuteTarget);
    }
    
    @Override
    public String functionBody(String methodName) {
        StringBuilder sb = new StringBuilder();
        
        // 添加try-catch块
        sb.append("try {\n");
        
        // 前置逻辑
        sb.append("    ").append(beforeLogic).append("\n");
        
        // 是否执行目标方法
        if (shouldExecuteTarget) {
            sb.append("    target.").append(methodName).append("();\n");
        } else {
            sb.append("    System.out.println(\"Around advice: 跳过目标方法执行\");\n");
        }
        
        // 后置逻辑
        sb.append("    ").append(afterLogic).append("\n");
        
        // 异常处理
        sb.append("} catch (Exception e) {\n");
        sb.append("    ").append(exceptionLogic).append("\n");
        sb.append("    throw new RuntimeException(e);\n");
        sb.append("}\n");
        
        return sb.toString();
    }
    
    @Override
    public void setMyInterface(MyInterface myInterfaceObject) throws NoSuchFieldException, IllegalAccessException {
        // 获取生成的代理类的Class对象
        Class<? extends MyInterface> aClass = myInterfaceObject.getClass();
        
        // 设置target字段
        Field targetField = aClass.getDeclaredField("target");
        targetField.setAccessible(true);
        targetField.set(myInterfaceObject, target);
    }
} 